package com.woniu.adopt.web;

import com.github.pagehelper.PageInfo;
import com.woniu.adopt.entity.User;
import com.woniu.adopt.result.ResultResponse;
import com.woniu.adopt.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

@Api(description = "前台用户管理接口")
@RestController
@RequestMapping("user")
public class UserController {

    @Autowired
    UserService userService;

    @ApiOperation(value = "前台用户列表")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataType = "int", name = "now", value = "查询的当前页", required = true, defaultValue = "1"),
            @ApiImplicitParam(paramType = "query", dataType = "int", name = "size", value = "每页显示多少条", required = true, defaultValue = "6"),
            @ApiImplicitParam(paramType = "query", dataType = "string", name = "name", value = "前台用户账号", required = false)
    })
    @GetMapping("/list")
    public ResultResponse<PageInfo<User>> rlist(@RequestParam(value = "now", defaultValue = "1") Integer now,
                                                @RequestParam(value = "size", defaultValue = "6") Integer size,
                                                String name) {
        ResultResponse<PageInfo<User>> result = null;

        try {
            PageInfo<User> rolePageInfo = userService.queryByPage(now, size, name);
            result = new ResultResponse<>(200, "查询成功!", rolePageInfo);
        } catch (RuntimeException e) {
            e.printStackTrace();
            result = new ResultResponse<>(201, "查询失败!");
        }
        return result;
    }

    @ApiOperation(value = "添加用户")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "username", value = "用户账户名", required = true),
            @ApiImplicitParam(name = "password", value = "密码", required = true),
            @ApiImplicitParam(name = "name", value = "后台用户昵称", required = true),
            @ApiImplicitParam(name = "telephone", value = "电话号码"),
            @ApiImplicitParam(name = "email", value = "电子邮箱"),
            @ApiImplicitParam(name = "gender", value = "用户性别", required = true),
            @ApiImplicitParam(name = "pic", value = "用户图片", required = true),
            @ApiImplicitParam(name = "age", value = "用户年龄", required = true)
    })
    @PostMapping("/add")
    public ResultResponse<Void> add(@RequestBody User user) {
        ResultResponse<Void> result = null;
        System.out.println("hello,world!");
        try {
            user.setUpdateTime(new Date());
            user.setInsertTime(new Date());
            userService.add(user);
            result = new ResultResponse<>(200, "添加成功!");
        } catch (RuntimeException e) {
            e.printStackTrace();
            result = new ResultResponse<>(201, "添加失败!");
        }
        return result;
    }

    @PostMapping("/edit")
    @ApiOperation("修改用户")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "用户ID", required = true),
            @ApiImplicitParam(name = "username", value = "用户账户名", required = true),
            @ApiImplicitParam(name = "password", value = "密码", required = true),
            @ApiImplicitParam(name = "name", value = "前台用户昵称", required = true),
            @ApiImplicitParam(name = "telephone", value = "电话号码"),
            @ApiImplicitParam(name = "email", value = "电子邮箱"),
            @ApiImplicitParam(name = "gender", value = "用户性别", required = true),
            @ApiImplicitParam(name = "pic", value = "用户图片", required = true),
            @ApiImplicitParam(name = "age", value = "用户年龄", required = true)
    })
    public ResultResponse<Void> edit(@RequestBody User user) {
        ResultResponse<Void> result = null;
        try {
            user.setUpdateTime(new Date());
            user.setInsertTime(new Date());
            userService.modify(user);
            result = new ResultResponse<>(200, "修改成功!");
        } catch (RuntimeException e) {
            e.printStackTrace();
            result = new ResultResponse<>(201, "修改失败!");
        }
        return result;
    }

    @ApiOperation(value = "根据用户ID删除用户")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", name = "id", value = "用户ID", required = true)
    })
    @DeleteMapping("/delete/{id}")
    public ResultResponse<Void> del(@PathVariable("id") int id) {
        ResultResponse<Void> result = null;
        try {
            userService.removeByKey(id);
            result = new ResultResponse<>(200, "删除成功");
        } catch (RuntimeException e) {
            e.printStackTrace();
            result = new ResultResponse<>(202, "后端写错了");
        }
        return result;
    }

    @ApiOperation(value = "根据用户ID切换锁定状态")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", name = "id", value = "用户ID", required = true)
    })
    @PostMapping("/changeLocked/{id}")
    public ResultResponse<Void> changeLocked(@PathVariable("id") int id) {
        ResultResponse<Void> result = null;
        try {
            userService.changeLocked(id);
            result = new ResultResponse<>(200, "状态切换成功");
        } catch (RuntimeException e) {
            e.printStackTrace();
            result = new ResultResponse<>(202, "后端写错了");
        }
        return result;
    }

    @ApiOperation(value = "新增用户时的重名验证")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataType = "string", name = "userName", value = "管理员的用户名", required = false)
    })
    @GetMapping("/judgedSameName")
    public ResultResponse<Void> judgedSameName(String userName) {
        ResultResponse<Void> result = null;

        try {
            int i = userService.judgedSameName(userName);
            String msg="用户名："+userName+"已被注册，请更换您的用户名";
            if(i!=0){
                result = new ResultResponse<>(201, msg);
            }else {
                result = new ResultResponse<>(200, "该用户名可以使用!");
            }
        } catch (RuntimeException e) {
            e.printStackTrace();
            result = new ResultResponse<>(202, "系统错误!");
        }
        return result;
    }
}
